package com.seeyon.apps.ddkk.controller;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.seeyon.apps.ddkk.manager.KkqxbManager;
import com.seeyon.apps.ddkk.manager.datasync.AbstractDataSyncManager;
import com.seeyon.apps.kk.controller.KkBaseController;
import com.seeyon.apps.kk.manager.AdapterSyncDataManager;
import com.seeyon.ctp.common.AppContext;



public class DDKkInfoController extends KkBaseController {
	// DD数据整合
	private AbstractDataSyncManager kkDdDataSync;
	private KkqxbManager kkqxbManager;

	

	public AbstractDataSyncManager getKkDdDataSync() {
		return kkDdDataSync;
	}

	public void setKkDdDataSync(AbstractDataSyncManager kkDdDataSync) {
		this.kkDdDataSync = kkDdDataSync;
	}

	public ModelAndView resendMail(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("apps/qiangs/resendMail");
		return mav;
	}

	// DD数据整合
	public ModelAndView loaddd(HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.kkDdDataSync.start();
		return this.ajaxDoneSuccess("同步结束================" + new Date());
	}
	
	// DD数据整合
	public ModelAndView sync(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, AdapterSyncDataManager> aMsgM = AppContext.getBeansOfType(AdapterSyncDataManager.class);
        for(Iterator<AdapterSyncDataManager> iter = aMsgM.values().iterator(); iter.hasNext();) {
        	try{
	        	AdapterSyncDataManager dataManager = iter.next();
	        	if(dataManager.enable()){
		        	dataManager.delHistoryData();
		        	List<Map> list = dataManager.getSyncData();
		        	if(list != null) {
			        	for(Map map: list){
			        		Map<String, Object> params = dataManager.data2FormMap(map);
			        		if(params == null){
			        			dataManager.saveOrUpdateData(map);
			        		} else {
			        			dataManager.saveOrUpdateData(params);
			        		}
		        			
			        	}
		        	}
	        	}
        	} catch (Exception e) {
    		}
        }
		return this.ajaxDoneSuccess("同步结束================" + new Date());
	}
	
	public ModelAndView syncDDBasicInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.kkqxbManager.syncDDBasicInfo();
		return this.ajaxDoneSuccess("同步结束================" + new Date());
	}
	
	
	public KkqxbManager getKkqxbManager() {
		return kkqxbManager;
	}

	public void setKkqxbManager(KkqxbManager kkqxbManager) {
		this.kkqxbManager = kkqxbManager;
	}	
	
	
	
}